package org.sercho.masp.notification.decision;

import java.util.List;

import org.sercho.masp.distribution.context.ContextConnection;
import org.sercho.masp.models.Context.InteractionResource;

/**
 * This is the controller of the decision-engine, which decides, on which
 * {@link InteractionResource}s the notification will be displayed and which
 * presentation-mode to use. This controller calls all deciders, which are
 * needed for the overall-decision.
 * 
 * @author Benny
 * 
 */
public class DecisionEngine {

	/**
	 * The only existing instance of the {@link DecisionEngine}.
	 */
	private static DecisionEngine instance;
	/**
	 * The decider for calculating the presentation-mode.
	 */
	private PresentationDecider presDec;
	/**
	 * The devider for calculating the {@link InteractionResource}s.
	 */
	private DeviceDecider devDec;

	/**
	 * Constructor.
	 */
	private DecisionEngine() {
		this.presDec = new PresentationDecider();
		this.devDec = new DeviceDecider();
	}

	/**
	 * Returns the only existing instance of the {@link DecisionEngine}. If none
	 * exist yet, it creates one.
	 * 
	 * @return the only {@link DecisionEngine}-instance
	 */
	public static DecisionEngine getInstance() {
		if (DecisionEngine.instance == null) {
			DecisionEngine.instance = new DecisionEngine();
		}
		return DecisionEngine.instance;
	}

	/**
	 * Calls every decider with the necessary parameters, inclusive those ones,
	 * which are return-parameters from other deciders.
	 * 
	 * @param messageId
	 *            the unique ID, which identifies a specific messageCategory of
	 *            a specific message-source
	 * @param userId
	 *            the ID of the user, which is the target of the message, for
	 *            which the notification shall be displayed
	 * @param timeOfNotificationId
	 *            the ID of the time, at which the message occurs
	 * @param urgency
	 *            the urgency of the message, if the category is not registered
	 *            yet and therefore no timeOfNotificationId is passed
	 * @param ctxCon
	 *            a {@link ContextConnection}-object, needed for the
	 *            resource-decision
	 * @return the {@link DecisionResult}, which contains the presentation-mode
	 *         and a {@link List} of the calculated {@link InteractionResource}s
	 */
	public DecisionResult decidePresentation(String messageId, String userId,
			String timeOfNotificationId, int urgency, ContextConnection ctxCon) {
		PresentationDecider presDecider = this.presDec;
		int presentationMode = presDecider.getPresentationMode(userId,
				messageId, timeOfNotificationId, urgency);
		DeviceDecider devDecider = this.devDec;
		List<InteractionResource> irList = devDecider.getNotificationDevices(
				ctxCon, userId, presentationMode);
		DecisionResult decision = new DecisionResult(presentationMode, irList);
		return decision;
	}

}
